
/*
 * readSetupFile.java
 *
 * Created on January 8, 2006, 12:05 AM
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

/**
 *
 * @author Sujal Bista
 */

import java.net.*;
import java.io.*;
import java.util.StringTokenizer;
import java.util.Vector;

public class readSetupFile
{
    //server Address
    private InetAddress serverAddress;

    //server connecting port number
    private int portNum;

    //list of groups and gender
    private Vector GroupGenderList;
    
    //list of Event Names
    private Vector EventNameList;
    
    //list of scoring system
    private Vector ScoringSystemList;
    

    /** Creates a new instance of readSetupFile */
    public readSetupFile()
    {
        String attrib;
        String value;
        String fileContent;
        int index;
        
         //list of groups and gender
        Vector AgeGroup = new Vector();
        Vector Level = new Vector();
        Vector Gender = new Vector();

        //list of groups and gender
        GroupGenderList = new Vector();

        //list of Event Names
        EventNameList = new Vector();

        //list of scoring system
        ScoringSystemList = new Vector();
        
        //list of events considered as bare hand
        globalData.BareHandEventName=new Vector();
        
        String fileNames[]={"setup.ini", "../data/setup.ini"};
        
        for(int i=0;i<fileNames.length;i++)
        {
            try
            {
                BufferedReader inputScript=new BufferedReader(new FileReader(fileNames[i]));

                do
                {
                    fileContent=inputScript.readLine();
                    if(fileContent==null)
                        break;
                    
                    index=fileContent.indexOf("//");
                    if(index!=-1)
                        fileContent=fileContent.substring(0, index);
                    
                    index=fileContent.indexOf('=');
                    if(index<0)
                        continue;

                    attrib=fileContent.substring(0, index).trim();
                    value=fileContent.substring(index+1).trim();

                    if(attrib.indexOf("Server Address")!=-1)
                    {
                        serverAddress = InetAddress.getByName(value);
                    }
                    else if(attrib.indexOf("Server IP")!=-1)
                    {
                        serverAddress = InetAddress.getByName(value);
                    }
                    else if(attrib.indexOf("Port No")!=-1)
                    {
                        portNum=Integer.valueOf(value);
                    }
                    else if(attrib.indexOf("Age Group Gender")!=-1)
                    {
                        GroupGenderList.addElement(value);
                    }
                    else if(attrib.indexOf("Age")!=-1)
                    {
                        AgeGroup.addElement(value);
                    }
                    else if(attrib.indexOf("Level")!=-1)
                    {
                        Level.addElement(value);
                    }
                    else if(attrib.indexOf("Gender")!=-1)
                    {
                        Gender.addElement(value);
                    }
                    else if(attrib.indexOf("Event Name")!=-1)
                    {
                        EventNameList.addElement(value);
                    }
                    else if(attrib.indexOf("Scoring System")!=-1)
                    {
                        ScoringSystemList.addElement(value);
                    }
                    else if(attrib.indexOf("Barehand Event")!=-1)
                    {
                        globalData.BareHandEventName.addElement(value);
                    }
                    else if(attrib.indexOf("JudgeA Time Range")!=-1)
                    {
                        try
                        {
                            globalData.JudgeATimeRangeInSec=Integer.parseInt(value);
                        }catch (NumberFormatException e) 
                        {

                        }
                    }
                    else if(attrib.indexOf("Display Score")!=-1)
                    {
                        if(value.toLowerCase().equals("true")==true)
                            globalData.displayScore=true;
                        else
                            globalData.displayScore=false;
                    }
                    else if(attrib.indexOf("Enable Modify Score")!=-1)
                    {
                        if(value.toLowerCase().equals("true")==true)
                            globalData.enableModifyScore=true;
                        else
                            globalData.enableModifyScore=false;
                    }
                    else if(attrib.indexOf("Title")!=-1)
                    {
                        globalData.titleName=value;
                    }
                    else if(attrib.indexOf("Team Selection")!=-1)
                    {
                        index=value.indexOf(':');
                        if(index<0)
                            continue;
                        
                        Vector eventG0=new Vector();
                        Vector eventG1=new Vector();
                        
                        String eventGroup0=value.substring(0, index).trim();
                        String eventGroup1=value.substring(index+1).trim();
                        
                        StringTokenizer tk0 = new StringTokenizer(eventGroup0, ",",false);
                        while (tk0.hasMoreTokens())
                        {
                            eventG0.add(tk0.nextToken().trim());
                        }    
                        StringTokenizer tk1 = new StringTokenizer(eventGroup1, ",",false);
                        while (tk1.hasMoreTokens())
                        {
                            eventG1.add(tk1.nextToken().trim());
                        }    
                        globalData.teamSelectionEventList0.add((eventG0));
                        globalData.teamSelectionEventList1.add((eventG1));
                        
//                        for(int ii=0; ii<eventG0.size();ii++)
//                        {
//                           System.out.print(((String)eventG0.get(ii))+" ");
//                        }
//                        System.out.print(" : ");
//                        
//                        for(int ii=0; ii<eventG1.size();ii++)
//                        {
//                           System.out.print(((String)eventG1.get(ii))+" ");
//                        }
//                        System.out.print("\n");                        
                        //System.out.print(eventGroup0 + " : " + eventGroup1+"\n");                        
                        //String[] eventGroup0Arr= new String
                        
                        
                    }
                    
                }while(true);

                inputScript.close();
            }catch(FileNotFoundException e)
            {
                  System.err.println("File \""+fileNames[i]+"\" not found. ");
                  System.exit(1);
            }
            catch(IOException e)
            {
                System.err.println(e.getMessage());
                System.exit(1);
            }
        }
//        String fileName="setup.ini";                
//        try
//        {
//            BufferedReader inputScript=new BufferedReader(new FileReader(fileName));
//
//            do
//            {
//                fileContent=inputScript.readLine();
//                if(fileContent==null)
//                    break;
//
//                index=fileContent.indexOf('=');
//                
//                if(index<0)
//                    continue;
//
//                attrib=fileContent.substring(0, index);
//                value=fileContent.substring(index+2);
//
//                if(attrib.indexOf("Server Address")!=-1)
//                {
//                    serverAddress = InetAddress.getByName(value);
//                }
//                else if(attrib.indexOf("Server IP")!=-1)
//                {
//                    serverAddress = InetAddress.getByName(value);
//                }
//                else if(attrib.indexOf("Port No")!=-1)
//                {
//                    portNum=Integer.valueOf(value);
//                }
//                else if(attrib.indexOf("Age Group Gender")!=-1)
//                {
//                    GroupGenderList.addElement(value);
//                }
//                else if(attrib.indexOf("Age")!=-1)
//                {
//                    AgeGroup.addElement(value);
//                }
//                else if(attrib.indexOf("Level")!=-1)
//                {
//                    Level.addElement(value);
//                }
//                else if(attrib.indexOf("Gender")!=-1)
//                {
//                    Gender.addElement(value);
//                }
//                else if(attrib.indexOf("Event Name")!=-1)
//                {
//                    EventNameList.addElement(value);
//                }
//                else if(attrib.indexOf("Scoring System")!=-1)
//                {
//                    ScoringSystemList.addElement(value);
//                }
//                else if(attrib.indexOf("Barehand Event")!=-1)
//                {
//                    globalData.BareHandEventName.addElement(value);
//                }
//                else if(attrib.indexOf("JudgeA Time Range")!=-1)
//                {
//                    try
//                    {
//                        globalData.JudgeATimeRangeInSec=Integer.parseInt(value);
//                    }catch (NumberFormatException e) 
//                    {
//                    
//                    }
//                }
//            }while(true);
//
//            inputScript.close();
//        }catch(FileNotFoundException e)
//        {
//              System.err.println("File \""+fileName+"\" not found. ");
//              System.exit(1);
//        }
//        catch(IOException e)
//        {
//            System.err.println(e.getMessage());
//            System.exit(1);
//        }


        //use age/level and gender to create group gender list
        for(int i=0;i<AgeGroup.size();i++)
        {
            for(int j=0;j<Level.size();j++)
            {
                for(int k=0;k<Gender.size();k++)
                {
                    String temp="/";
                    String data=(String)AgeGroup.elementAt(i)+temp+(String)Level.elementAt(j)+temp+(String)Gender.elementAt(k);
                    GroupGenderList.addElement(data);
                }
            }
        }

        //list of age group level and gender allowed in the tournament
        globalData.AgeGroupList=new String[AgeGroup.size()];
        //add string from the setup file
        for(int i=0;i<AgeGroup.size();i++)
        {
            String data=(String)AgeGroup.elementAt(i);
            globalData.AgeGroupList[i]=data;
        }

        globalData.LevelList=new String[Level.size()];
        //add string from the setup file
        for(int i=0;i<Level.size();i++)
        {
            String data=(String)Level.elementAt(i);
            globalData.LevelList[i]=data;
        }

        globalData.GenderList=new String[Gender.size()];
        //add string from the setup file
        for(int i=0;i<Gender.size();i++)
        {
            String data=(String)Gender.elementAt(i);
            globalData.GenderList[i]=data;
        }
       
        //add group Gender List
        //allocate space
        globalData.groupInfo=new String[GroupGenderList.size()+1];
        //add blank string
        globalData.groupInfo[0]=new String();
        //add string from the setup file
        for(int i=0;i<GroupGenderList.size();i++)
        {
            String data=(String)GroupGenderList.elementAt(i);
            globalData.groupInfo[i+1]=data;
        }
         
        //add Event Name List
        //allocate space
        globalData.eventInfo=new String[EventNameList.size()+1];
        //add blank string
        globalData.eventInfo[0]=new String();
        //add string from the setup file
        for(int i=0;i<EventNameList.size();i++)
        {
            String data=(String)EventNameList.elementAt(i);
            globalData.eventInfo[i+1]=data;
        }
        
        //add Event Name List
        //allocate space
        globalData.scoringSystemInfo=new globalData.ScoringSystem[ScoringSystemList.size()+1];
        //add string from the setup file
        globalData.scoringSystemInfo[0]=new globalData.ScoringSystem("","","");
        for(int i=0;i<ScoringSystemList.size();i++)
        {
            String data=(String)ScoringSystemList.elementAt(i);
            
            index=data.indexOf(',');
            if(index<0) continue;
            String eventName=data.substring(0, index);
            data=data.substring(index+2);

            index=data.indexOf(',');
            if(index<0) continue;
            String level=data.substring(0, index);
            String scoringType=data.substring(index+2);

            globalData.scoringSystemInfo[i+1]=new globalData.ScoringSystem(eventName,level,scoringType);
        }

        
        //clear all list
        GroupGenderList.clear();
        EventNameList.clear();
        ScoringSystemList.clear();

    }

    //returns server name
    public InetAddress getServerName()
    {
        return serverAddress;
    }

    //returns port number
    public int getPortNum()
    {
        return portNum;
    }

}
